/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import com.opensymphony.xwork2.validator.annotations.DoubleRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import com.paris5.master2.guitarmaster.bean.EcomBid;
import com.paris5.master2.guitarmaster.bean.EcomProduct;
import com.paris5.master2.guitarmaster.bean.EcomUser;
import com.paris5.master2.guitarmaster.exception.EcomException;
import com.paris5.master2.guitarmaster.exception.EcomQueryException;
import com.paris5.master2.guitarmaster.service.ServiceProductRemote;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author Alice
 */
public class Encherir extends EcomAction implements ServletRequestAware {

    private EcomProduct product;
    private EcomBid bid;
    private int idProduct;
    private float montant;
    HttpServletRequest request;
            
    public void setIdProduct(int idProduct) {
        this.idProduct = idProduct;
    }

    public int getIdProduct() {
        return idProduct;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
    /**
     *
     * @return
     */
    @DoubleRangeFieldValidator(message = "Montant incorrect")
    public float getMontant() {
        return montant;
    }

    public void setMontant(float montant) {
        this.montant = montant;
    }

    public EcomProduct getProduct() {
        return product;
    }

    public void setProduct(EcomProduct product) {
        this.product = product;
    }

    public EcomBid getBid() {
        return bid;
    }

    public void setBid(EcomBid bid) {
        this.bid = bid;
    }

    @Override
    public String execute() {
        try {

            EcomUser ecomUser = (EcomUser) getFromSession("USER");
            ServiceProductRemote serviceProductRemote = lookupServiceProductRemote();
            idProduct = (int) Double.parseDouble(request.getParameter("idProduct"));
            if (ecomUser.getCountry().getCode().equals("EN"))
            {
                double rate = (Double)getFromSession("RATE");
                float rate2 = (float)rate;
                montant = montant / rate2;
            }
            product = serviceProductRemote.getProduct(idProduct);
            bid = serviceProductRemote.getLastProductPriceInBid(product, 1, true);
            if (montant > bid.getBidPrice()) {
                serviceProductRemote.bid(ecomUser.getId(), idProduct, montant);
                setMessage("Montant enregistré");
                return SUCCESS;
            } else {
                setMessage("Montant incorrect");
                return INPUT;
            }
            
        } catch (EcomQueryException ex) {
            Logger.getLogger(Encherir.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EcomException ex) {
            Logger.getLogger(Encherir.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ERROR;


    }
}
